const fs = require('fs');
const path = require('path');

// 获取当前工作目录
const currentDir = process.cwd();

function changeApp(type) {
    if(type === 'anhao'){
        // 修改 project.config.json
        const projectConfigPath = path.join(currentDir, 'project.config.json');
        let projectConfig = JSON.parse(fs.readFileSync(projectConfigPath, 'utf8'));
        projectConfig.appid = "wx56c4069a51e02dc8";
        fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2));

        // 修改 src/constants/index.ts
        const constantsPath = path.join(currentDir, 'src/constants/index.ts');
        let constantsContent = fs.readFileSync(constantsPath, 'utf8');
        constantsContent = constantsContent.replace(/export const Prefix = '.*?';/, "export const Prefix = 'api-ah';");
        constantsContent = constantsContent.replace(/export const AppName = '.*?';/, "export const AppName = 'anhao';");
        
        fs.writeFileSync(constantsPath, constantsContent);

        const appConfigPath = path.join(currentDir, 'src/app.config.ts');
        let appConfigContent = fs.readFileSync(appConfigPath, 'utf8');
        appConfigContent = appConfigContent.replace(/navigationBarTitleText: '.*?',/, "navigationBarTitleText: '广西安好护理',");
        
        fs.writeFileSync(appConfigPath, appConfigContent);
        
        console.log('✅ 已切换到 anhao 配置');
    }
    else if(type === 'huasheng'){
        // 修改 project.config.json
        const projectConfigPath = path.join(currentDir, 'project.config.json');
        let projectConfig = JSON.parse(fs.readFileSync(projectConfigPath, 'utf8'));
        projectConfig.appid = "wx77728b3bf1f20d3d";
        fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2));

        // 修改 src/constants/index.ts
        const constantsPath = path.join(currentDir, 'src/constants/index.ts');
        let constantsContent = fs.readFileSync(constantsPath, 'utf8');
        constantsContent = constantsContent.replace(/export const Prefix = '.*?';/, "export const Prefix = 'api';");
        constantsContent = constantsContent.replace(/export const AppName = '.*?';/, "export const AppName = 'huasheng';");

        fs.writeFileSync(constantsPath, constantsContent);

        const appConfigPath = path.join(currentDir, 'src/app.config.ts');
        let appConfigContent = fs.readFileSync(appConfigPath, 'utf8');
        appConfigContent = appConfigContent.replace(/navigationBarTitleText: '.*?',/, "navigationBarTitleText: '华声康护',");
        
        fs.writeFileSync(appConfigPath, appConfigContent);
        
        console.log('✅ 已切换到 huasheng 配置');
    }
    else {
        console.log('❌ 无效的参数，请使用 "anhao" 或 "huasheng"');
        console.log('使用方法: node change.js <anhao|huasheng>');
        process.exit(1);
    }
}

// 从命令行获取参数
const type = process.argv[2];

if (!type) {
    console.log('❌ 请提供参数');
    console.log('使用方法: node change.js <anhao|huasheng>');
    process.exit(1);
}

changeApp(type);